package fr.enseirb.mycub.db;

import java.util.ArrayList;
import org.json.JSONException;

import fr.enseirb.mycub.db.structure.ParkingDAO;
import android.database.DatabaseUtils.InsertHelper;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import android.util.Log;

public class ParkingDBHelper {

	// insertion des parkings
	public static void insertParkings(SQLiteDatabase mDb,
			ArrayList<ParkingDAO> jsonParking) throws JSONException,
			SQLiteException {
		InsertHelper parkingInsertHelper = new InsertHelper(mDb,
				ParkingDBConstants.FREE_PARKING_TABLE);
		final int idColumn = parkingInsertHelper
				.getColumnIndex(BaseColumns._ID);
		final int nameColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_NAME);
		final int openColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_OPEN);
		final int addressColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_ADDRESS);
		final int latitudeColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_LATITUDE);
		final int longitudeColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_LONGITUDE);
		final int favoriteColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_FAVORITE);
		final int placeColumn = parkingInsertHelper
				.getColumnIndex(ParkingDBConstants.KEY_PLACES);
		final int size = jsonParking.size();		
		final String sql = "INSERT INTO "
				+ ParkingDBConstants.FREE_PARKING_TABLE
				+ " VALUES (?,?,?,?,?,?,?,?);";

		parkingInsertHelper.close();
		
//		Log.i("Insert Station", "init des colonnes...");
		try {
			mDb.beginTransaction();
			SQLiteStatement insert = mDb.compileStatement(sql);
			int id;
			String name;
			Log.i("Insert parking", "debut de la boucle... ( taille : "+size+" ) ");
			for (int i = 0; i < size; i++) {
				ParkingDAO jsonItem = jsonParking.get(i);
				id = jsonItem.getId();
				name = jsonItem.getName();
				insert.bindLong(idColumn, id);
				insert.bindString(nameColumn, name);
				insert.bindLong(openColumn, jsonItem.isOpen() ? 1 : 0);
				insert.bindString(addressColumn, jsonItem.getAddress());
				insert.bindLong(latitudeColumn,
						(int) (jsonItem.getmLatitude() * 1E6));
				insert.bindLong(longitudeColumn,
						(int) (jsonItem.getmLongitude() * 1E6));
				insert.bindLong(favoriteColumn, 0); // Favorite
				insert.bindLong(placeColumn,jsonItem.getPlaces());
				insert.executeInsert();

			}
			mDb.setTransactionSuccessful();
		} catch (SQLException e) {
			throw e;
		} finally {
			mDb.endTransaction();
		}
	}


	public static void cleanAndInsertParkings(SQLiteDatabase mDb,
			ArrayList<ParkingDAO> jsonParking) throws JSONException {
		mDb.delete(ParkingDBConstants.FREE_PARKING_TABLE, null, null);
		insertParkings(mDb, jsonParking);
	}

	public static int getParkingCount(SQLiteDatabase mDb) throws SQLException {
		Cursor cursor = mDb.rawQuery("SELECT COUNT(*) AS count FROM "
				+ ParkingDBConstants.FREE_PARKING_TABLE,null);
		cursor.moveToNext();
		int count = cursor.getInt(0);
		cursor.close();
		return count;
	}

	public static ArrayList<Cursor> getFilteredParkingCursor(SQLiteDatabase mDb,String[] projection,
			String where,
			String orderBy) {
		String nWhere;
		nWhere = where;
		ArrayList<Cursor> cursors = new ArrayList<Cursor>();
		cursors.add(mDb.query(ParkingDBConstants.FREE_PARKING_TABLE, projection, nWhere,
				null, null, null,
				orderBy));
		return cursors;
	}

	public static Cursor getParking(SQLiteDatabase mDb, int id, String[] columns) throws SQLException {
		Cursor cursor = mDb.query(true, ParkingDBConstants.FREE_PARKING_TABLE, columns,
				BaseColumns._ID + " = ?",
				new String[] {
				String.valueOf(id) },
				null, null, null, null);
		if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
			throw new SQLException("No Parking found with ID " + id);
		}
		return cursor;
	}
}
